package com.fordmotor.aspectj;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer;
import com.fordmotor.constant.GaodeEnum;
import com.fordmotor.pojo.GaodeObject;
import com.fordmotor.util.MapUtil;
import com.google.common.collect.Maps;
import com.nimbusds.jose.util.JSONObjectUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.EnableCaching;

import static com.fordmotor.cache.EhCache.cacheManager;

import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author THUNDEROBOT
 * @date 2020/07/07 12:15
 */
@Slf4j
@EnableCaching      //支持缓存
@CacheConfig(cacheNames="sinopec,ford,member")
public class DefaultControllerHandler extends AbstractControllerHandler {
    protected Method method;
    public DefaultControllerHandler(JoinPoint joinPoint) {
        super(joinPoint);
    }

    @Override
    protected Object handle(JoinPoint joinPoint,Object rvt) throws Throwable {
        Signature signature = this.joinPoint.getSignature();
        this.methodName = signature.getName();
        if (signature instanceof MethodSignature) {
            MethodSignature methodSignature = (MethodSignature)signature;
            try {
                this.method = joinPoint.getTarget().getClass().getMethod(this.methodName, methodSignature.getParameterTypes());
                if (null != this.method) {
                    // handle(this.methodName,args);
                    return  handle(methodName,rvt) ;
                   // handle(proceedingJoinPoint);
                }
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public Object handle(String methodName,Object rvt) throws Throwable {
        long timestamp = System.currentTimeMillis();
//        this.logIn();
        net.minidev.json.JSONObject object = JSONObjectUtils.parse(JSONObject.toJSONString(rvt));
        String uid = JSONObjectUtils.getString(object,"uid");
        net.minidev.json.JSONArray array = JSONObjectUtils.getJSONArray(object,"list");
        List<GaodeObject> list =JSONArray.parseArray(net.minidev.json.JSONArray.toJSONString(array),GaodeObject.class);
        if(GaodeEnum.SINOPEC.getValue().equals(methodName)){
            Ehcache cache = cacheManager.getCache("sinopec");
            Element uObject = cache.getQuiet(uid);
            if(uObject != null && uObject.getObjectValue() != null){
               Object valueObject = uObject.getObjectValue();
               Map<String,Integer> map =  (Map<String,Integer>)(valueObject);
                for(GaodeObject obj:list){
                    String id = obj.getId();
                    if(null != map.get(id) && map.get(id) > 0){
                        map.put(id,map.get(id)+1);
                    }else{
                        map.put(id,1);
                    }
                }
            }else{
                Map<String,Integer> eValue = Maps.newHashMap();
                for(GaodeObject obj:list){
                    String id = obj.getId();
                    eValue.put(id,1);
                }
                Element element = new Element(uid,eValue);
                cache.put(element);
             //   Cache sinopecCache = new Cache(uid, 50000, true, true, 0, 0);
            }
        }
       else if(GaodeEnum.FORD.getValue().equals(methodName)){
            Ehcache cache = cacheManager.getCache("ford");
            Element uObject = cache.getQuiet(uid);
            if(uObject != null && uObject.getObjectValue() != null){
                Object valueObject = uObject.getObjectValue();
                Map<String,Integer> map = (Map<String,Integer>)(valueObject);
                for(GaodeObject obj:list){
                    String id = obj.getId();
                    if(null != map.get(id) && map.get(id) > 0){
                        map.put(id,map.get(id)+1);
                    }else{
                        map.put(id,1);
                    }
                }
            }else{
                Map<String,Integer> eValue = Maps.newHashMap();
                for(GaodeObject obj:list){
                    String id = obj.getId();
                    eValue.put(id,1);
                }
                Element element = new Element(uid,eValue);
                cache.put(element);
                //   Cache sinopecCache = new Cache(uid, 50000, true, true, 0, 0);
            }
        }
         return null;
    }

}